package com.cgc.shop.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.cgc.shop.model.Category;
import com.cgc.shop.service.CategoryService;

@Service("categoryService")
public class CategoryServiceImpl extends BaseServiceImpl<Category> implements CategoryService {

	@SuppressWarnings("unchecked")
	@Override
	public List<Category> queryJoinAccount(String name, int pageIndex, int size) {
		String hql = "from Category c left join fetch c.account where c.name like :name";  
        return getSession().createQuery(hql)  
                .setString("name", "%" + name + "%")
                .setFirstResult((pageIndex-1) * size)
                .setMaxResults(size)
                .list();
	}

	@Override
	public Long getCount(String name) {
		String hql = "select count(*) from Category c where c.name like :name";
		return (Long) getSession().createQuery(hql)  
	            .setString("name", "%" + name + "%")  
	            .uniqueResult(); //返回一条记录:总记录数
	}

	@Override
	public void deleteByIds(String ids) {
		String hql = "delete Category c where c.id in (" + ids + ")";
		getSession().createQuery(hql).executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Category> queryByPopular(Boolean popular) {
		String hql = "from Category c where c.popular=:popular";
		return getSession().createQuery(hql)
				.setBoolean("popular", popular)
				.list();
	}

	

}
